"use strict";
cc._RF.push(module, '0d9fcAHaU1Po6hgHAN5Ghbs', 'XmlHttpTool');
// Script/XmlHttpTool.js

"use strict";

var _PlatformUtil = require("./PlatformUtil");

var _PlatformUtil2 = _interopRequireDefault(_PlatformUtil);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var base64 = require('./Tool/base64.js');

var httpUtils = {

  serverPath: "https://api.taoqi001.com/wxmp",

  //结果需要解密的请求
  httpPost: function httpPost(url, dataDic, _cbSuccess, _cbFail) {

    //创建xhr对象 
    var xhr = new XMLHttpRequest();
    //设置xhr请求的超时时间
    xhr.timeout = 3000;
    // //设置响应返回的数据格式
    //        xhr.responseType ="application/json";
    var sendData = _PlatformUtil2.default.platform.params;
    // var sendData = {'appid':'com.taoqi001.wawaji','appver':117,'devid':1,'devtype': 1,'token': "e0786b7f7bf2c28c5e552a8be17a3ac3"};
    var fullData = {};

    fullData = Object.assign({}, sendData, dataDic);

    // console.log("拼接的字典",fullData)


    //创建一个 post 请求，采用异步
    xhr.open('POST', this.serverPath + url, true);

    //注册相关事件回调处理函数
    xhr.onreadystatechange = function (e) {

      if (xhr.readyState == 4 && xhr.status == 200) {

        if (JSON.parse(xhr.responseText).errcode == 0) {

          typeof _cbSuccess == "function" && _cbSuccess(receiveDataDecode(JSON.parse(xhr.responseText).data)); //如果是个函数执行后面
        } else {

          typeof _cbFail == "function" && _cbFail(JSON.parse(xhr.responseText)); //如果是个函数执行后面
        }
      }
    };

    xhr.ontimeout = function (e) {};
    xhr.onerror = function (e) {

      console.log("网络请求错误");
    };
    xhr.onprogress = function (e) {}
    // console.log("加载进度")


    //发送数据
    ;xhr.send(JSON.stringify(fullData));
  },

  TestPost: function TestPost() {
    console.log("测试请求");
  },

  //获取token的请求
  getTokenPost: function getTokenPost(url, dataDic, _cbSuccess, _cbFail) {

    //创建xhr对象 
    var xhr = new XMLHttpRequest();
    //设置xhr请求的超时时间
    xhr.timeout = 3000;
    // //设置响应返回的数据格式
    //        xhr.responseType ="application/json";
    //  var sendData = PlatformUtil.platform.params;
    var sendData = { 'appid': 'com.taoqi001.mp_marmot', 'appver': 168, 'devid': "6", 'devtype': 2, 'token': "" };
    var fullData = {};

    fullData = Object.assign({}, sendData, dataDic);

    //创建一个 post 请求，采用异步
    xhr.open('POST', this.serverPath + url, true);

    //注册相关事件回调处理函数
    xhr.onreadystatechange = function (e) {

      if (xhr.readyState == 4 && xhr.status == 200) {

        if (JSON.parse(xhr.responseText).errcode == 0) {

          typeof _cbSuccess == "function" && _cbSuccess(JSON.parse(xhr.responseText).data); //如果是个函数执行后面
        } else {

          typeof _cbFail == "function" && _cbFail(JSON.parse(xhr.responseText)); //如果是个函数执行后面
        }
      }
    };

    xhr.ontimeout = function (e) {};
    xhr.onerror = function (e) {

      console.log("网络请求错误");
    };
    xhr.onprogress = function (e) {}
    // console.log("加载进度")


    //发送数据
    ;xhr.send(JSON.stringify(fullData));
  },

  NhttpPost: function NhttpPost(url, dataDic, _cbSuccess, _cbFail) {

    //创建xhr对象 
    var xhr = new XMLHttpRequest();
    //设置xhr请求的超时时间
    xhr.timeout = 3000;
    // //设置响应返回的数据格式
    //        xhr.responseType ="application/json";
    var sendData = _PlatformUtil2.default.platform.params;
    console.log("取出来字典", sendData);
    // var sendData = {'appid':'com.taoqi001.wawaji','appver':117,'devid':1,'devtype': 1,'token': "e0786b7f7bf2c28c5e552a8be17a3ac3"};
    var fullData = {};

    fullData = Object.assign({}, sendData, dataDic);

    //创建一个 post 请求，采用异步
    xhr.open('POST', this.serverPath + url, true);

    //注册相关事件回调处理函数
    xhr.onreadystatechange = function (e) {

      if (xhr.readyState == 4 && xhr.status == 200) {

        //  console.log("解密出的数据",JSON.parse(xhr.responseText))

        if (JSON.parse(xhr.responseText).errcode == 0) {

          typeof _cbSuccess == "function" && _cbSuccess(JSON.parse(xhr.responseText).data); //如果是个函数执行后面
        } else {

          typeof _cbFail == "function" && _cbFail(JSON.parse(xhr.responseText)); //如果是个函数执行后面
        }
      }
    };

    xhr.ontimeout = function (e) {};
    xhr.onerror = function (e) {

      console.log("网络请求错误");
    };
    xhr.onprogress = function (e) {}
    // console.log("加载进度")


    //发送数据
    ;xhr.send(JSON.stringify(fullData));
  },

  ThttpPost: function ThttpPost(url, dataDic, _cbSuccess, _cbFail) {

    //创建xhr对象 
    var xhr = new XMLHttpRequest();
    //设置xhr请求的超时时间
    xhr.timeout = 3000;
    // //设置响应返回的数据格式
    //        xhr.responseType ="application/json";
    console.log("交互的字典2", _PlatformUtil2.default.platform.params);
    var sendData = _PlatformUtil2.default.platform.params;
    // var sendData = {'appid':'com.taoqi001.wawaji','appver':117,'devid':1,'devtype': 1,'token': "e0786b7f7bf2c28c5e552a8be17a3ac3"};
    var fullData = {};

    fullData = Object.assign({}, sendData, dataDic);

    //创建一个 post 请求，采用异步
    xhr.open('POST', this.serverPath + url, true);

    //注册相关事件回调处理函数
    xhr.onreadystatechange = function (e) {

      if (xhr.readyState == 4 && xhr.status == 200) {

        if (JSON.parse(xhr.responseText).errcode == 0) {
          console.log("请求正确", JSON.parse(xhr.responseText));
          typeof _cbSuccess == "function" && _cbSuccess(JSON.parse(xhr.responseText)); //如果是个函数执行后面
        } else {
          console.log("请求求错误", JSON.parse(xhr.responseText));
          typeof _cbFail == "function" && _cbFail(JSON.parse(xhr.responseText)); //如果是个函数执行后面
        }
      }
    };

    xhr.ontimeout = function (e) {};
    xhr.onerror = function (e) {

      console.log("网络请求错误");
    };
    xhr.onprogress = function (e) {
      console.log("加载进度");
    };

    //发送数据
    xhr.send(JSON.stringify(fullData));
  }

};

var base64_encode = function base64_encode(str) {
  return base64.Base64.encode(str);
};

var base64_decode = function base64_decode(str) {
  return base64.Base64.decode(str);
};

var alice = function alice(str, key) {
  //xor加密

  var output = '';
  var newKey = key.split("").reverse().join(""); //逆序key
  var keyLen = newKey.length - 1; //忽略key最后一个字符
  for (var i = 0; i < str.length; i++) {
    var j = i % keyLen;
    output += String.fromCharCode(str.charCodeAt(i) ^ newKey.charCodeAt(j));
  }

  return output;
};
var toAlice = function toAlice(str, key) {

  return base64_encode(alice(str, key));
};
var fromAlice = function fromAlice(str, key) {
  return alice(base64_decode(str), key);
};

var receiveDataDecode = function receiveDataDecode(data) {

  var key;
  var decodeData;

  key = toAlice(_PlatformUtil2.default.platform.params["appid"], String(_PlatformUtil2.default.platform.params["appver"]));

  // key = toAlice("com.taoqi001.wawaji", "117");

  decodeData = JSON.parse(fromAlice(data, key));

  return decodeData;
};

module.exports = httpUtils;

cc._RF.pop();